<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DBGen
 *
 * @author vlad
 */
class DBGen extends DBGenerator {
    
    private $_database;
    
    public function __construct(SQLDatabase $struct, $path = "") {
        if (!empty($struct)) {$this->_database = $struct;}
        if (empty($path)) {$path = SCORE_APP_LIB_PATH."php/db/";}
        $this->AddPath($path.$this->_database."/", 'db');
        return $this;
    }
    
    private function prepareDatabaseClassName($className) {
        return "database_".$className;
    }
    
    private function prepareDatabaseFileName($fileName) {
        return "database_".$fileName;
    } 
    
    public function Generate() {
        try {
            $this->HumanizeClassNames = false;
            if ($this->CreateAllPaths()) {
                // create DBNameBase
                
                $className = $this->HumanizeClassName($this->prepareDatabaseClassName($this->_database));
                $baseClassName = $this->HumanizeClassName($this->prepareDatabaseClassName($this->AddBaseSuffix($this->_database)));
                
                $fileName = $this->prepareDatabaseFileName($this->_database);
                $baseFileName = $this->AddBaseSuffix($fileName);
                
                $fullFileName = $this->GetPath('db').$fileName;
                $fullBaseFileName = $this->GetPath('db').$baseFileName;
                
                // craete base classes
                $fileContent = <<<EOF
class $className extends $baseClassName {

}

EOF;
                
                $baseFileContent = <<<EOF
class $baseClassName {

}

EOF;
                
                $this->CreatePHPFile($fullFileName, $fileContent);
                $this->CreatePHPFile($fullBaseFileName, $baseFileContent);
                // create DBName
                
                // create Tables
                foreach($this->_database->GetTables() as $table) {
                    $TableGen = new TableGen($table, $this->GetPath('db'));
                    $TableGen->Generate();
                }
            }
        } catch (Exception $e) {
            throw ErrorException($e->getMessage());
        }
        return $this;
    }
    
}
    
    
